﻿namespace BindTryParseMVC.Models
{
    public class DateRangeTP
    {
        public DateOnly? From { get; set; }
        public DateOnly? To { get; set; }

        public DateRangeTP(string from,string to)
        {
             if(string.IsNullOrEmpty(from))
                throw new ArgumentNullException(nameof(from));
            if (string.IsNullOrEmpty(to))
                throw new ArgumentNullException(nameof(to));
            From = DateOnly.Parse(from);
            To = DateOnly.Parse(to);
        }

        public static bool TryParse(string?value,out DateRangeTP? dateRange)
        {
            var range = value?.Split(',',StringSplitOptions.RemoveEmptyEntries|StringSplitOptions.TrimEntries);
            if(range?.Length != 2)
            {
                dateRange = default;
                return false;
            }

            dateRange = new DateRangeTP(range[0], range[1]);
            return true;
        }
    }
}
